﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 原料发货录入 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            int id = 0;
            if (!this.IsPostBack)
            {
                //权限管理子程序==========================================================
                string opp3 = (string)Session["OPP3"];
                if (opp3 == null || opp3 == ",")
                {
                    this.LeiBie.DataBind();
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    this.DS_LeiBie.SelectCommand = "select ID, [品名]+' '+[钢级]+' '+[规格] AS 名称 FROM 原料 where [ID] in (" + opp3 + ")";
                }
                this.LeiBie.DataBind();
                //权限管理子程序==========================================================
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }

                if (id > 0)
                {
                    this.Label_ID.Text = id.ToString();
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = this.Label_ID.Text;
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
                else
                {
                    DateTime d = DateTime.Today;
                    this.Date1.Text = d.ToLongDateString();

                    string list = Request.QueryString["list"];
                    if (list == null || list == "")
                    {
                         return;
                    }
                    string[] tmp = list.Split(new char[] { ',' });
                    int t;
                    for (int i = 0; i < tmp.Length; i++)
                    {
                        if (!int.TryParse(tmp[i], out t))
                        {
                            return;
                        }
                        else if (t <= 0)
                        {
                            return;
                        }
                    }
                    this.MingXi.Text = list;
                    this.SqlDataSource1.SelectParameters["ID"].DefaultValue = "0";
                    this.SqlDataSource1.SelectCommand =
@"declare @总重量 numeric(18,3)=0,@总标重 numeric(18,3)=0,@总支数 int=0,@车号列表 varchar(1024)='';
declare cur cursor for
select 原料ID,供货ID,分厂,库区,cast(磅重 as float),cast(标重 as float),支数,车号 from 原料到货记录 where ID in(" + list + @");
open cur;
fetch next from cur into @原料ID,@供货ID,@分厂,@库区,@重量,@标重,@支数,@车号;
while @@FETCH_STATUS=0
begin
set @总重量=@总重量+@重量;
set @总支数=@总支数+@支数;
set @总标重=@总标重+@标重;
set @车号列表=@车号列表+','+@车号;
fetch next from cur into @原料ID,@供货ID,@分厂,@库区,@重量,@标重,@支数,@车号;
end
close cur;
deallocate cur;
set @重量=@总重量;
set @支数=@总支数;
set @标重=@总标重;
set @车号=SUBSTRING(@车号列表,2,1024);";
                    this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                }
            }

        }



        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            if (this.Label_ID.Text == "0")
            {
                this.SqlDataSource1.InsertParameters["保管员"].DefaultValue = op;
                this.SqlDataSource1.Insert();
            }
            else
            {
                if (this.SqlDataSource1.Update() > 0)
                {
                    this.Notice = "·保存成功";
                }
            }
        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.LeiBie.Text = ((int)e.Command.Parameters[1].Value).ToString();

           

            this.GongHuo.Text = ((int)e.Command.Parameters[2].Value).ToString();

            if (!Convert.IsDBNull(e.Command.Parameters[3].Value))
            {
                this.FenChang.Text = ((int)e.Command.Parameters[3].Value).ToString();
            }

            if (!Convert.IsDBNull(e.Command.Parameters[4].Value))
            {
                this.KuQu.Text = ((int)e.Command.Parameters[4].Value).ToString();
            }

            if (!Convert.IsDBNull(e.Command.Parameters[5].Value))
            {
                this.Date1.Text = ((DateTime)e.Command.Parameters[5].Value).ToLongDateString();
            }

            if (!Convert.IsDBNull(e.Command.Parameters[6].Value))
            {
                this.ZhongLiang.Text = (((double)e.Command.Parameters[6].Value)).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[7].Value))
            {
                this.ZhiShu.Text = ((int)e.Command.Parameters[7].Value).ToString();
            }
            if (!Convert.IsDBNull(e.Command.Parameters[8].Value))
            {
                this.BeiZhu.Text = (string)e.Command.Parameters[8].Value;
            }
            if (!Convert.IsDBNull(e.Command.Parameters[10].Value))
            {
                this.CheHao.Text = (string)e.Command.Parameters[10].Value;
            }

            if (!Convert.IsDBNull(e.Command.Parameters[11].Value))
            {
                this.BiaoZhong.Text = (((double)e.Command.Parameters[11].Value)).ToString();
            }
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            this.Label_ID.Text = e.Command.Parameters[0].Value.ToString();

            this.Notice = "·保存成功";
        }
    }
}
